package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.callback.LanguageCallback;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //菜品列表分頁查詢
    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //StringUtils.isNotEmpty(name) 判断name是否为非空字符串
        //1.从菜品表根据name分页查询
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //2.执行菜品列表分页查询
        page = dishMapper.selectPage(page, wrapper);
        //遍历
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //根据菜品的分类id从分类表中查询分类信息(名称)
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
            //从口味表根据菜品id查询口味表
           /* LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavors);*/

        }
        return page;
    }

    @Override
    public void save(Dish dish) {
        //保存dish表基本信息
        dishMapper.insert(dish);
        //遍历口味表
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //为口味设置菜品所属id
                dishFlavor.setDishId(dish.getId());
                //保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //根据菜品id从菜品表查询基本信息
        Dish dish = dishMapper.selectById(id);
        //根据category_id去分类表获取分类名称
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //根据菜品id从口味表查询当前口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //根据菜品id直接更新菜品基本信息
        dishMapper.updateById(dish);
        //删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish_id = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);
        //将修改之后口味列表重新保存到口味表中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());//设置菜品id
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public void startOrEnd(Long[] ids, Integer status) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //判断要删除的 菜品是否停售
        //delete count(0) dish where id in ids and status = 1;
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("要删除的菜品在在售状态，不能删除");
        }
        for (Long id : ids) {
            //根据菜品id删除口味列表
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper1);
            dishMapper.deleteById(id);
        }
    }
    @Override
    public List<Dish> findList(Long categoryId,Integer status,String name) {
        //select * from dish where categoryId = #{categoryId} and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1)
                .like(StringUtils.isNotEmpty(name),Dish::getName, name);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品的id从口味表中查询菜品口味信息
                //select * from dish_flavor where dish_id = #{dish.id}
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> flavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(flavorList);
            }
        }
        return dishList;
    }

}
